/*
 * @Author: Robin
 * @Date: 2020/6/29 16:11
 * @LastEditors: Robin
 * @LastEditTime: 2021-07-29 16:43:59
 * @Description: TMC生成默认配置文件
 */
package main

import (
	"io/ioutil"
	"log"
	"strconv"
	"time"

	"gopkg.in/yaml.v2"
)

type YamlFile struct {
	File struct {
		FileName       string `yaml:"file_name"`
		MoldNo         string `yaml:"mold_no"`
		HotRunnerId    string `yaml:"hot_runner_id"`
		PersonInCharge string `yaml:"person_in_charge"`
		Remark         string `yaml:"remark"`
		CreatedAt      string `yaml:"created_at"`
		UpdatedAt      string `yaml:"updated_at"`
	}
	Zones  []YamlZone  `yaml:"zones"`
	Timers []YamlTimer `yaml:"timers"`
	SPMs   []YamlSPM   `yaml:"spms"`
}

type YamlZone struct {
	// 软件自定义界面部分
	Number     int    `yaml:"number"`      // 测温点编号
	Name       string `yaml:"name"`        // 节点名称
	NameApp    bool   `yaml:"name_app"`    // 是否应用节点名称
	MaxTemp    int    `yaml:"max_temp"`    // 显示的最大温度
	MaxCurrent int    `yaml:"max_current"` // 显示的最大电流
	// modbus
	SV           uint16 `yaml:"sv"`            // 设定温度
	OnOff        bool   `yaml:"on_off"`        // 开关状态
	JK           uint16 `yaml:"jk"`            // 热电偶型号
	CF           uint16 `yaml:"cf"`            // 温度单位
	PWM_SSR      uint16 `yaml:"pwm_ssr"`       // PWM SSR
	Soft         bool   `yaml:"soft"`          // 软启动开关
	Mode         uint16 `yaml:"mode"`          // 工作模式
	Turn         bool   `yaml:"turn"`          // 自整定
	TempUpper    uint16 `yaml:"temp_upper"`    // 温度上限
	TempLower    uint16 `yaml:"temp_lower"`    // 温度下限
	StbyTime     uint16 `yaml:"stby_time"`     // 待机时间
	StbyOut      uint16 `yaml:"stby_out"`      // 待机温度百分比
	CurrentUpper uint16 `yaml:"current_upper"` // 电流上限
	CurrentLower uint16 `yaml:"current_lower"` // 电流下限
	SoftTime     uint16 `yaml:"soft_time"`     // 软启动输出时间
	SoftOut      uint16 `yaml:"soft_out"`      // 软启动输出百分比
	PID_P        uint16 `yaml:"pid_p"`         // PID P
	PID_I        uint16 `yaml:"pid_i"`         // PID I
	PID_D        uint16 `yaml:"pid_d"`         // PID D
	ManualOut    uint16 `yaml:"manual_out"`    // 手动输出百分比
	Time         uint16 `yaml:"time"`          // 采样时间
	Frequency    uint16 `yaml:"frequency"`     // 电网频率
	Voltage      uint16 `yaml:"voltage"`       // 电网电压
	Reverse      bool   `yaml:"reverse"`       // 接反检测
	ManualSelect uint16 `yaml:"manual_select"` // 手动输出选择
	// 520 04寄存器
	// PV      uint16 `yaml:"pv"`      // 实际温度
	// Current uint16 `yaml:"current"` // 输出电流
	// Output  uint16 `yaml:"output"`  // 输出百分比
	// PWR     uint16 `yaml:"pwr"`     // 输出功率
	// State   uint16 `yaml:"state"`   // 工作状态
	PVSync           bool   `yaml:"pv_sync"`            // PV同步开关
	PVSyncMode       int    `yaml:"pv_sync_mode"`       // PV同步工作模式 0-自动 1-手动
	PVSyncManual     bool   `yaml:"pv_sync_manual"`     // PV手动同步选择
	PVSyncPercent    int    `yaml:"pv_sync_percent"`    // PV同步手动百分比
	CurrentLimit     bool   `yaml:"current_limit"`      // 电流限制开关
	CurrentLimitMode int    `yaml:"current_limit_mode"` // 电流限制工作模式 0-自动 1-手动
	CurrentSet       uint16 `yaml:"current_set"`        // 电流限制
	// CurrentAuto      int  `yaml:"current_auto"`       // 自动电流限制
	// CurrentManual    int  `yaml:"current_manual"`     // 手动电流限制
	// CurrentAutoSet   int  `yaml:"current_auto_set"`   // 电流限制,仅自动有效
	// CurrentManualSet int  `yaml:"current_manual_set"` // 电流限制,仅手动有效
	Phase int `yaml:"phase"` // 电流相位设置，一块板子上面的所有点电流相位都是一样的
	// CurrentMax       int    `yaml:"current_max"`        // 检测到的最大电流值
	Ready           bool   `yaml:"ready"`             // 就绪检测
	ReadyTemp       uint16 `yaml:"ready_temp"`        // 就绪温度
	SignalCtrl      bool   `yaml:"signal_ctrl"`       // 注塑信号控制
	SignalDelayTime int    `yaml:"signal_delay_time"` // 注塑信号延迟时间
	SignalStopTime  int    `yaml:"signal_stop_time"`  // 注塑信号停止时间
}

type YamlTimer struct {
	CH uint16 `yaml:"ch"` // CH
	// Check     int `yaml:"check"`     // 是否选中
	Mode        uint16 `yaml:"mode"`         // Mode
	Frequency   uint16 `yaml:"frequency"`    // Frequency
	Unit        uint16 `yaml:"unit"`         // Unit
	Delay1      uint16 `yaml:"delay1"`       // Delay1
	Open1       uint16 `yaml:"open1"`        // Open1
	Delay2      uint16 `yaml:"delay2"`       // Delay2
	Open2       uint16 `yaml:"open2"`        // Open2
	SignalDelay uint16 `yaml:"signal_delay"` // 注塑信号延迟时间
}

type YamlSPM struct {
	CH uint16 `yaml:"ch"` // CH
	// Check      int `yaml:"check"`       // 是否选中
	Mode       uint16 `yaml:"mode"`        // Mode
	Frequency  uint16 `yaml:"frequency"`   // Frequency
	Open1      uint16 `yaml:"open1"`       // Open1
	Close1     uint16 `yaml:"close1"`      // Close1
	Open2      uint16 `yaml:"open2"`       // Open2
	Close2     uint16 `yaml:"close2"`      // Close2
	DelayOpen  uint16 `yaml:"delay_open"`  // delay open
	DelayClose uint16 `yaml:"delay_close"` // delay close
}

func main() {
	var yf YamlFile
	yf.File.FileName = "TMC"
	yf.File.MoldNo = "0000000000"
	yf.File.HotRunnerId = "0000000000"
	yf.File.PersonInCharge = "Robin"
	yf.File.Remark = "Default file, cannot be edited"
	yf.File.CreatedAt = time.Now().Format("2006-01-02 15:04:05")
	yf.File.UpdatedAt = time.Now().Format("2006-01-02 15:04:05")

	var zone YamlZone
	// 循环测温点设置
	phase := 1 // 电流相位设置
	for i := 0; i < 240; i++ {
		// 计算相位
		if i != 0 && i%4 == 0 {
			if phase == 3 {
				phase = 1
			} else {
				phase++
			}
		}
		zone.Number = i
		zone.Name = "ZONE" + strconv.Itoa(i+1)
		zone.NameApp = false
		zone.MaxTemp = 500
		zone.MaxCurrent = 2000
		zone.SV = 200
		zone.OnOff = false
		zone.JK = 1
		zone.CF = 0
		zone.PWM_SSR = 1
		zone.Soft = false
		zone.Mode = 1
		zone.Turn = false
		zone.TempUpper = 200
		zone.TempLower = 200
		zone.StbyTime = 60
		zone.StbyOut = 50
		zone.CurrentUpper = 2000
		zone.CurrentLower = 0
		zone.SoftTime = 1
		zone.SoftOut = 500
		zone.PID_P = 60
		zone.PID_I = 60
		zone.PID_D = 15
		zone.ManualOut = 500
		zone.Time = 10
		zone.Frequency = 50
		zone.Voltage = 220
		zone.ManualSelect = 0
		zone.PVSync = false
		zone.PVSyncMode = 0
		zone.PVSyncManual = false
		zone.PVSyncPercent = 50
		zone.CurrentLimit = false
		zone.CurrentLimitMode = 0
		// zone.CurrentAuto = 32000
		// zone.CurrentManual = 32000
		// zone.CurrentAutoSet = 1000
		// zone.CurrentManualSet = 1000
		zone.CurrentSet = 1000
		zone.Phase = phase
		zone.Ready = false
		zone.ReadyTemp = 10
		zone.SignalCtrl = false
		zone.SignalDelayTime = 0
		zone.SignalStopTime = 0

		yf.Zones = append(yf.Zones, zone)
	}

	var timer YamlTimer
	// timer设置
	for i := uint16(0); i < 32; i++ {
		timer.CH = i
		timer.Mode = 4
		timer.Frequency = 1
		timer.Unit = 100
		timer.Delay1 = 0
		timer.Open1 = 0
		timer.Delay2 = 0
		timer.Open2 = 0
		yf.Timers = append(yf.Timers, timer)
	}

	var spm YamlSPM
	// spm设置
	for i := uint16(0); i < 32; i++ {
		spm.CH = i
		spm.Mode = 4
		spm.Frequency = 1
		spm.Open1 = 0
		spm.Close1 = 0
		spm.Open2 = 0
		spm.Close2 = 0
		spm.DelayOpen = 0
		spm.DelayClose = 0

		yf.SPMs = append(yf.SPMs, spm)
	}
	byts, err := yaml.Marshal(&yf)
	if err != nil {
		log.Fatalln(err.Error())
	}
	err = ioutil.WriteFile("./TMC.yml", byts, 0666)
	if err != nil {
		log.Fatalln(err.Error())
	}
}
